package services

import (
	"encoding/json"
	"gorm.io/gorm"
	"mygo/app/models"
	"mygo/app/tools"
	"strings"
)

func HandelHsyc(forecast *models.Forecast, time string, db *gorm.DB) {
	timeArr := strings.Split(time, "-")
	year := timeArr[0]
	month := timeArr[1]
	day := timeArr[2]
	timeNow := year + "-" + month + "-" + day
	var foreList models.ForeList
	var raData = models.Forecast{}
	if db.Where("code = ?", "rayg").First(&foreList).RowsAffected > 0 {
		var foreDetail models.ForeDetailJson
		db.Where("foreListId = ? AND time = ? AND state != ?", foreList.Id, timeNow, "0").First(&foreDetail)
		json.Unmarshal(foreDetail.Data, &raData)
	}

	var foreList1 models.ForeList
	var wzData = models.Forecast{}

	if db.Where("code = ?", "wzhy").First(&foreList1).RowsAffected > 0 {
		var foreDetail1 models.ForeDetailJson
		db.Where("foreListId = ? AND time = ? AND state != ?", foreList1.Id, timeNow, "0").First(&foreDetail1)
		json.Unmarshal(foreDetail1.Data, &wzData)
	}
	for i, report := range forecast.Reports {
		if i == 0 {
			if len(raData.Reports) != 0 {
				report.Data[1][1].Value = raData.Reports[1].Data[1][4].Value
				waveValueArr := tools.GetNumFloat64(raData.Reports[1].Data[1][4].Value)
				if len(waveValueArr) == 2 {
					if getWaveGrade(waveValueArr[0]) == getWaveGrade(waveValueArr[1]) {
						report.Data[1][2].Value = getWaveGrade(waveValueArr[0])
					} else {
						report.Data[1][2].Value = getWaveGrade(waveValueArr[0]) + "到" + getWaveGrade(waveValueArr[1])
					}
				} else if len(waveValueArr) == 1 {
					report.Data[1][2].Value = getWaveGrade(waveValueArr[0])
				}
			}
			if len(wzData.Reports) != 0 {
				if len(strings.Split(wzData.Reports[1].Data[3][3].Value, "℃")) == 2 {
					report.Data[1][3].Value = tools.Float64ToFixedStr(tools.StrToFloat64(strings.Split(wzData.Reports[1].Data[3][3].Value, "℃")[0])+0.1, 1) + "℃"
				}
			}
		}
	}
}
